﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROS.Application.DomainObjects;
using ROS.Persistence.DAO;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence
{
    internal class OrderDataMapper : IDataMapper<Order>
    {
        #region Fields

        IOrderDAO orderDAO;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the order DAO.
        /// </summary>
        private IOrderDAO OrderDAO
        {
            get
            {
                if (orderDAO == null)
                {
                    DAOFactory daoFactory = DAOFactory.GetDAOFactory("MySQL");
                    orderDAO = daoFactory.GetOrderDAO();
                }
                return orderDAO;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new OrderDataMapper object.
        /// </summary>
        public OrderDataMapper()        
        {
            orderDAO = null;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Create a new order.
        /// </summary>
        /// <param name="order"></param>
        public void Create(Order order)
        {
            OrderDTO orderDTO = LoadOrderDTOFromOrder(order);
            OrderDAO.Insert(orderDTO);
            order.ID = orderDTO.ID;
        }

        public Order FindByID(long id)
        {
            OrderDTO orderDTO = OrderDAO.SelectByID(id);
            return LoadOrderFromOrderDTO(orderDTO);
        }

        public IList<Order> FindAll()
        {
            IList<OrderDTO> orderDTOs = OrderDAO.SelectAll();
            List<Order> orders = new List<Order>();
            foreach (OrderDTO orderDTO in orderDTOs)
            {
                orders.Add(LoadOrderFromOrderDTO(orderDTO));
            }
            return orders;
        }

        private OrderDTO LoadOrderDTOFromOrder(Order order)
        {
            OrderDTO orderDTO = new OrderDTO();
            orderDTO.ID = order.ID;
            orderDTO.TableNumber = order.TableNumber;
            orderDTO.GuestCount = order.GuestCount;
            orderDTO.Notes = order.Notes;
            return orderDTO;
        }

        private Order LoadOrderFromOrderDTO(OrderDTO orderDTO)
        {
            Order order = new Order(orderDTO.ID,
                                    orderDTO.TableNumber,
                                    orderDTO.GuestCount,
                                    orderDTO.Notes);
            return order;
        }

        public void Update(Order domainObject)
        {
            throw new NotImplementedException();
        }

        public void Delete(Order domainObject)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
